package com.regvision.contacts.dialog;

import org.apache.commons.logging.Log;

import com.regvision.contacts.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BaseDialog extends Dialog implements View.OnClickListener {

	protected View mDialogView;

	public static BaseDialog instance;
	
	private static int mOrientation;
	
	protected TextView mTvTitle;
	protected LinearLayout mLlContent;
	
	protected Button mBtnCancel;
	protected Button mBtnConfirm;
	
	public BaseDialog(Context context) {
		super(context, R.style.DialogTran);
		init();
	}

	public BaseDialog(Context context, int theme) {
		super(context, theme);
		init();
	}

	protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.height = ViewGroup.LayoutParams.MATCH_PARENT;
		params.width = ViewGroup.LayoutParams.MATCH_PARENT;
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
	}
	
	protected void init() {
		
		mDialogView = View.inflate(getContext(), R.layout.dialog_base, null);
		
		mBtnCancel = (Button) mDialogView.findViewById(R.id.btn_dialog_cancel);
		mBtnConfirm = (Button) mDialogView.findViewById(R.id.btn_dialog_confirm);

		mBtnCancel.setOnClickListener(this);
		mBtnConfirm.setOnClickListener(this);
		
		mTvTitle = (TextView) mDialogView.findViewById(R.id.tv_title_dialog);
		
		mLlContent = (LinearLayout) mDialogView.findViewById(R.id.ll_dialog_content);
		
		setContentView(mDialogView);
		
	}
	
	public static BaseDialog getInstance(Context context) {

		int ort = context.getResources().getConfiguration().orientation;
		if (mOrientation != ort) {
			mOrientation = ort;
			instance = null;
		}

		if (instance == null || ((Activity) context).isFinishing()) {
			synchronized (BaseDialog.class) {
				if (instance == null) {
					instance = new BaseDialog(context);
				}
			}
		}
		return instance;
	}
	
	public void setTitle(String title) {
		this.mTvTitle.setText(title);
	}
	public void setTitle(int resid) {
		this.mTvTitle.setText(resid);
	}

	public void setCancelText(String text) {
		this.mBtnCancel.setText(text);
	}
	public void setCancelText(int resid) {
		this.mBtnCancel.setText(resid);
	}

	public void setConfirmText(String text) {
		this.mBtnConfirm.setText(text);
	}
	public void setConfirmText(int resid) {
		this.mBtnConfirm.setText(resid);
	}

	@Override
	public void onClick(View v) {
		
		dismiss();
		
		switch (v.getId()) {
		case R.id.btn_dialog_cancel:
			if (mCancelClickListeren != null) {
				mCancelClickListeren.onCancelClick();
			}
			break;
		case R.id.btn_dialog_confirm:
			if (mConfirmClickListeren != null) {
				mConfirmClickListeren.onConfirmClick();
			}
			break;

		default:
			break;
		}
	}
	
	
	//=========================
	// 取消、确定按钮监听接口
	//==========================
	private OnCancelClickListeren mCancelClickListeren;
	private OnConfirmClickListeren mConfirmClickListeren;
	public void setOnCancelClickListeren(OnCancelClickListeren listeren) {
		mCancelClickListeren = listeren;
	}
	public void setOnConfirmClickListeren(OnConfirmClickListeren listeren) {
		mConfirmClickListeren = listeren;
	}
	public interface OnCancelClickListeren {
		public void onCancelClick();
	}
	public interface OnConfirmClickListeren {
		public void onConfirmClick();
	}
}
